<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p @click="add">{{num}}</p>
    <p>{{oneComp}}</p>
  </div>
</template>

<script>
import {
  reactive,
  computed,
  onMounted,
  ref,
  toRefs,
  watch,
  watchEffect
} from "vue";

export default {
  name: "Start",
  props: {
    msg: String
  },
  setup() {
    onMounted(() => {
      console.log("mounted");
    });
    let { num, oneComp, add } = useNum();
    return { num, oneComp, add };
  }
};

function useNum() {
  let state = reactive({
    num: 1
  });

  let temp = ref(0);

  let oneComp = computed(() => {
    return state.num * 2;
  });
  function add() {
    state.num++;
    temp.value++;
  }
  watch(
    () => state.num,
    (v1, v2) => {
      console.log("num changed:" + v1 + " " + v2);
    }
  );
  watchEffect(() => {
    console.log("curr value:" + state.num);
  });

  watchEffect(() => {
    console.log("temp value:" + temp.value);
  });
  return { ...toRefs(state), oneComp, add };
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
